package com.have.blog.admin.controller;


import com.have.blog.admin.annotation.operationLogger.OperationLogger;
import com.have.blog.admin.domain.dto.BlogSubjectDto;
import com.have.blog.admin.service.AdminBlogSubjectService;
import com.have.blog.base.exception.ThrowableUtils;
import com.have.blog.base.validator.group.GetList;
import com.have.blog.base.validator.group.Insert;
import com.have.blog.base.validator.group.Update;
import com.have.blog.base.wrapper.CommonPage;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.core.annotion.avoidRepeatableCommit.AvoidRepeatableCommit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 后台博客专题管理
 */
@Api(value = "专题相关接口", tags = {"专题相关接口"})
@RestController
@RequestMapping("/subject")
@Slf4j
public class AdminBlogSubjectController {
    @Autowired
    private AdminBlogSubjectService subjectService;

    @ApiOperation(value = "获取专题列表", notes = "获取专题列表")
    @PostMapping("/getList")
    public Wrapper<CommonPage> getList(@Validated({GetList.class}) @RequestBody BlogSubjectDto blogSubjectDto, BindingResult result) {

        ThrowableUtils.checkParamArgument(result);
        List<BlogSubjectDto> list = subjectService.getList(blogSubjectDto);
        return Wrapper.success(CommonPage.restPage(list));
    }

    @AvoidRepeatableCommit
    @OperationLogger(value = "增加专题")
    @ApiOperation(value = "增加专题", notes = "增加专题")
    @PostMapping("/add")
    public Wrapper add(@Validated({Insert.class}) @RequestBody BlogSubjectDto blogSubjectDto, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return subjectService.addSubject(blogSubjectDto);
    }

    @OperationLogger(value = "编辑专题")
    @ApiOperation(value = "编辑专题", notes = "编辑专题")
    @PostMapping("/edit")
    public Wrapper edit(@Validated({Update.class}) @RequestBody BlogSubjectDto blogSubjectDto, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return subjectService.editBlogSubject(blogSubjectDto);
    }

    @OperationLogger(value = "批量删除专题")
    @ApiOperation(value = "批量删除专题", notes = "批量删除专题")
    @PostMapping("/deleteBatch")
    public Wrapper delete(@RequestBody List<Integer> uids) {
        // 参数校验
        log.info("批量删除:{}", uids);
        return subjectService.deleteBatchSubject(uids);
    }
}
